package org.example2;

import java.lang.reflect.Method;

public class CustomerProxyFactory implements CustomerInvocationHandler {

    private IPerson target;

    public CustomerProxyFactory(IPerson target) {
        this.target = target;
    }

    public IPerson getInstance(IPerson target) {
        this.target = target;
        Class<?> clazz = target.getClass();
        return (IPerson) CustomerProxy.newProxyInstance(new CustomerClassLoader(), clazz.getInterfaces(), this);
    }

    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        before();
        Object result = method.invoke(this.target, args);
        after();
        return result;
    }

    private void after() {
        System.out.println("CustomerProxyFactory类after方法执行了----做一些业务操作");
    }

    private void before() {
        System.out.println("CustomerProxyFactory类before方法执行了----做一些业务操作");
    }
}